package com.library.weiget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jtjrenren.android.taxi.driver.R;
import com.library.utils.DensityUtils;


/**
 * Created by wdl on 2015/12/11.
 */
public class CustomeDialog extends Dialog {

    public CustomeDialog(Context context) {
        super(context);
    }

    public CustomeDialog(Context context, int theme) {
        super(context, theme);
    }

    protected CustomeDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String postButtonText;
        private String negativeButtonText;
        private OnClickListener postListener;
        private OnClickListener negativeListener;

        public Builder(Context context){
            this.context=context;
        }


        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setPostButton(String postButtonText, OnClickListener listener) {
            this.postButtonText = postButtonText;
            this.postListener = listener;
            return this;
        }

        public Builder setPostButtonText(String postButtonText) {
            this.postButtonText = postButtonText;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeListener = listener;
            return this;
        }

        public Builder setNegativeButtonText(String negativeButtonText) {
            this.negativeButtonText = negativeButtonText;
            return this;
        }

        public Builder setPostListener(OnClickListener postListener) {
            this.postListener = postListener;
            return this;
        }

        public Builder setNegativeListener(OnClickListener negativeListener) {
            this.negativeListener = negativeListener;
            return this;
        }

        public CustomeDialog create(){
            final CustomeDialog dialog=new CustomeDialog(context, R.style.dlg_common);
            View view = View.inflate(context, R.layout.dlg_custome_common, null);


            int width = DensityUtils.getScreenW(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (width * 0.8),
                    LinearLayout.LayoutParams.WRAP_CONTENT);

            dialog.addContentView(view,new ViewGroup.LayoutParams(params));

            TextView tv_title=(TextView) dialog.findViewById(R.id.dlg_custome_title);
            if (!(title==null)&&!("".equals(title))){
                tv_title.setVisibility(View.VISIBLE);
                tv_title.setText(title);
            }else{
                tv_title.setVisibility(View.GONE);
            }

            if (!(message==null)&&!("".equals(message))){
                ((TextView) dialog.findViewById(R.id.dlg_custome_content)).setText(message);
            }

            Button bt_post= (Button) dialog.findViewById(R.id.dlg_custome_ok);
            if (postButtonText==null||"".equals(postButtonText)){
                bt_post.setVisibility(View.GONE);
            }else{
                bt_post.setText(postButtonText);
                if (postListener!=null){
                    bt_post.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            postListener.onClick(dialog,DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            }

            Button bt_cancle= (Button) dialog.findViewById(R.id.dlg_custome_cancle);
            if (negativeButtonText==null||"".equals(negativeButtonText)){
                bt_cancle.setVisibility(View.GONE);
            }else{
                bt_cancle.setText(negativeButtonText);
                if (negativeListener!=null){
                    bt_cancle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            negativeListener.onClick(dialog,DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            }

            return  dialog;
        }
    }
}
